package com.itfetter.cart.listener;


import com.itfetter.api.dto.ItemDTO;
import com.itfetter.cart.service.ICartService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.messaging.Message;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@RocketMQMessageListener(consumerGroup = "rocketmq-consumer-cart", topic = "cart-clear")
public class CartClearListener implements RocketMQListener<Message<List<ItemDTO>>> {
    private final ICartService cartService;

    @Override
    public void onMessage(Message<List<ItemDTO>> message) {
        log.info("消息消费成功，开始清空购物车商品");
        Collection<Long> itemIds = message.getPayload().stream().map(e -> e.getId()).collect(Collectors.toList());
        Long userId=(Long)message.getHeaders().get("userId");
//        Long userId = null;
//        for (Long aLong : longListMap.keySet()) {
//            userId = aLong;
//        }
//        List<Long> itemIds = null;
//        Collection<ItemDTO> shops = longListMap.get(userId);
//        for (ItemDTO shop : shops) {
//            itemIds.add(shop.getId());
//        }
//        itemIds = longListMap.get(userId).stream().map(e -> e.getId()).collect(Collectors.toList());
        cartService.removeByUserIdandItemids(userId, itemIds);
    }
}

